package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import basemod.ReflectionHacks;
import basemod.helpers.CardModifierManager;
import basemod.interfaces.OnPlayerTurnStartSubscriber;
import com.megacrit.cardcrawl.actions.common.MakeTempCardInDrawPileAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.actions.ChooseOneAddToDrawPileAction;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class ImmuneDecline extends AbstractInvincibleCard implements OnPlayerTurnStartSubscriber {
    public static final String ID = InvincibleOneMod.makeID("ImmuneDecline");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/ImmuneDecline.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.RARE;
    private static final CardTarget TARGET = CardTarget.SELF;

    private static final int COST = -2;

    public ImmuneDecline() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.modifier = new ExhaustAtStartOfCombatMod();
        this.tags.add(CardTagsEnum.ALERT);
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
    }

    @Override
    public boolean onBattleStart() {
        BaseMod.unsubscribe(this);
        BaseMod.subscribe(this);
        return true;
    }

    @Override
    public void onRemoveFromMasterDeck() {
        super.onRemoveFromMasterDeck();
        BaseMod.unsubscribeLater(this);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void receiveOnPlayerTurnStart() {
        if (upgraded) {
            addToBot(new ChooseOneAddToDrawPileAction(c -> c.hasTag(CardTagsEnum.VIRUS)));
        } else {
            CardRarity rarity1 = ReflectionHacks.privateStaticMethod(AbstractDungeon.class, "getCardRarityFallback", int.class).invoke(new Object[]{AbstractDungeon.cardRandomRng.random(100)});
            List<AbstractCard> pool = BaseMod.getCustomCardsToAdd().stream().filter(card -> card.hasTag(CardTagsEnum.VIRUS) && !card.tags.contains(CardTags.HEALING)).collect(Collectors.toList());
            Collections.shuffle(pool, new Random(AbstractDungeon.cardRandomRng.randomLong()));
            AbstractCard virus = pool.stream().filter(card -> card.rarity == rarity1).findFirst().orElseGet(SporeSpray::new);
            addToBot(new MakeTempCardInDrawPileAction(virus, 1, true, true, false));
        }
    }
}
